<?php


namespace app\model;

use think\facade\Db;
use think\Model;
use app\common\library\Translation;
use app\model\RemarkModel;
use app\model\Attachment as AttachmentModel;
use app\model\ConfigModel;
use app\model\AfterSalesHandleModel;

class AfterSalesModel extends Model
{
    protected $table = "store_after_sales";

    /**
     * @param $param
     * @return AfterSalesModel|array|mixed|string|Model|null
     * @notes 售后反馈 提交
     * @author: dongdong
     * @date: 2025/1/13 9:49
     */
    public function submit_after_sales($param){
        Db::startTrans();

        try {
            $data['create_at'] = date('Y-m-d H:i:s');
            $data['user_id'] = $param['user_id'];
            $data['delivery_id'] = $param['delivery_id'];
            $data['after_sales_type'] = $param['after_sales_type'];
            $data['undertake_user_id'] = $param['undertake_user_id'];
            $situation = $param['situation']??'';
            $ask = $param['ask']??'';

            $insertGetId = self::insertGetId($data);

            $AttachmentModel = new AttachmentModel();
            $str = '';
            if ($param['images']) {
                $list_arr = explode(',', $param['images']);
                foreach ($list_arr as $k => &$v) {
                    $str .= $AttachmentModel->insertGetId(['user_id' => $param['user_id'], 'table_name' => $this->table, 'table_id' => $insertGetId, 'path' => $v]) . ',';
                }
                $str = trim($str, ',');
            }

            $RemarkModel = new RemarkModel();
            $situation_id = 0;
            if(!empty($situation)){
                $situation_id = $RemarkModel::remark_id($situation, $this->table, $insertGetId);
            }
            $ask_id = 0;
            if(!empty($ask)){
                $ask_id = $RemarkModel::remark_id($ask, $this->table, $insertGetId);
            }

            self::where(["id" => $insertGetId])->update(['attachment_id' => $str, 'situation_id' => $situation_id, 'ask_id' => $ask_id]);
            Db::table("store_delivery")->where(['id'=>$param['delivery_id']])->update(['status'=>5]);
            Db::table("store_delivery_attach")->where(['id'=>$param['delivery_id']])->update(['after_sales_time'=>date('Y-m-d H:i:s')]);
            $car = Db::table("store_car")->where(['order_id'=>$data['delivery_id']])->find();
            if($car){
                Db::table("store_car")->where(['id'=>$car['id']])->update(['order_id'=>0,'status'=>1,'send_jb_time'=>0]);
            }
            Db::commit();
            return self::find([$insertGetId]);
        }catch (\Exception $e){
            _d($e);
            Db::rollback();
            return '';
        }
    }


    /**
     * @param $param
     * @return AfterSalesModel|array|mixed|Model|null
     * @notes 处理情况列表
     * @author: dongdong
     * @date: 2025/1/13 13:55
     */
    public function handling_situation($param){
        try {
            $find = self::where(['id'=>$param['after_sales_id']])->where(['user_id'=>$param['user_id']])->find();
            $ConfigModel = new ConfigModel();
            $RemarkModel = new RemarkModel();
            $AttachmentModel = new AttachmentModel();
            $AfterSalesHandleModel = new AfterSalesHandleModel();
            if($find){
                $find['after_sales_config'] = $ConfigModel->info($find['after_sales_type']);
                $find['situation'] = $RemarkModel->info($find['situation_id']);
                $find['ask'] = $RemarkModel->info($find['ask_id']);
                $find['attachment'] = $AttachmentModel->info($this->table,$find['id']);
                $find['undertake_user'] = Db::connect("fast_mysql")->table("fa_user")->where(['id'=>$find['undertake_user_id']])->field("id,username,username_ru,mobile,level,avatar")->find();
                $find['list'] = $AfterSalesHandleModel->lists($param['after_sales_id']);
            }
            return $find;
        }catch (\Exception $e){
            return [];
        }
    }


    /**
     * @param $param
     * @return int
     * @notes 协商一致 完成售后
     * @author: dongdong
     * @date: 2025/1/13 14:49
     */
    public function complete_after_sales($param){
        $store_after_sales = Db::table("store_after_sales")->where(['id'=>$param['after_sales_id']])->find();
        if($store_after_sales){
            $store_after_sales_handle = Db::table("store_after_sales_handle")->where(['after_sales_id'=>$param['after_sales_id'],'handle_type'=>26])->order("id desc")->value('handle_money');
            $handle_money = 0;
            if($store_after_sales_handle){
                $handle_money = $store_after_sales_handle;
            }

            $delivery_id = Db::table("store_after_sales")->where(['id'=>$param['after_sales_id']])->value('delivery_id');
            Db::table("store_delivery")->where(['id'=>$delivery_id])->update(['status'=>4]);
            Db::table("store_delivery_attach")->where(['delivery_id'=>$delivery_id])->update(['handle_money'=>$handle_money,'complete_at'=>date('Y-m-d H:i:s')]);
            self::where(['id'=>$param['after_sales_id']])->update(['status'=>1,'complete_at'=>date('Y-m-d H:i:s'),'complete_user_id'=>$param['user_id']]);
        }

        return 1;
    }
}